package com.zjh.photoshopstudy.app.login;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.reflect.TypeToken;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.Callback;
import com.lzy.okgo.model.Progress;
import com.lzy.okgo.model.Response;
import com.lzy.okgo.request.base.Request;
import com.zjh.photoshopstudy.Model.AppUserEntity;
import com.zjh.photoshopstudy.app.login.LoginPresent.LoginCallBack;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

import static com.zjh.photoshopstudy.Utils.Data.webRoot_URL;

public class LoginHttp {

    public void login(final LoginCallBack callBack, final String username, final String password) {
        String url = webRoot_URL + "/appUser/login.html";
        OkGo.<String>get(url)//
                .tag(this)//
                .params("uAccountName", username)//
                .params("uPassword", password)
                .execute(new Callback<String>() {
                    @Override
                    public void onStart(Request<String, ? extends Request> request) {

                    }

                    @Override
                    public void onSuccess(Response<String> response) {
                        Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd HH:mm:ss").create();
                        Map<String, Object> map = gson.fromJson(response.body(), new TypeToken<Map<String, Object>>() {
                        }.getType());
                        String message = (String) map.get("message");
                        if (message.equals("登录成功")) {
                            Map<String, Object> map2 = (Map<String, Object>) ((Map<String, Object>) map.get("data")).get("appUserEntity");
                            AppUserEntity userInfo = new AppUserEntity();
                            userInfo.setuPassword(password);
                            userInfo.setuAccountName(username);
                            userInfo.setId(((Double) map2.get("id")).longValue());
                            userInfo.setuIcon((String) map2.get("uIcon"));
                            userInfo.setuName((String) map2.get("uName"));
                            userInfo.setuSex(((Double)map2.get("uSex")).intValue());
                            DateFormat bf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                            try {
                                userInfo.setuBirthday(bf.parse((String) map2.get("uBirthday")));
                            } catch (ParseException e) {
                                e.printStackTrace();
                            }
                            callBack.onLoginSuccess(userInfo);
                        } else {
                            callBack.onLoginFail(message);
                        }

                    }

                    @Override
                    public void onCacheSuccess(Response<String> response) {

                    }

                    @Override
                    public void onError(Response<String> response) {

                    }

                    @Override
                    public void onFinish() {

                    }

                    @Override
                    public void uploadProgress(Progress progress) {

                    }

                    @Override
                    public void downloadProgress(Progress progress) {

                    }

                    @Override
                    public String convertResponse(okhttp3.Response response) throws Throwable {
                        return response.body().string();
                    }
                });
    }
}
